<template>

  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
    <el-col>
      <strong v-if="isRoot"><i class="el-icon-s-home" />{{ treeData.name }}</strong>
      <span v-else>{{ treeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addDepartments">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="editDepartments">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delDepartments(treeData.id)">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>

</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    treeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      required: true
    }
  },
  methods: {
    // 点击操作里面的删除
    async delDepartments(id) {
      // 调用删除接口
      await this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await delDepartments(id)
      // 告诉父组件重新发送请求渲染数据
      this.$emit('delDepartments')
      this.$message.success('删除成功')
    },
    // 点击操作里面的添加子部门
    addDepartments() {
      this.$emit('addDepartments', this.treeData)
    },
    // 点击编辑
    editDepartments() {
      this.$emit('editDepartments', this.treeData)
    }
  }
}
</script>

